package com.liushao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liushao.entity.TransFlow;
import com.liushao.entity.UserInfo;
import com.liushao.entity.bo.TransFlowBO;
import com.liushao.mapper.TransFlowMapper;
import com.liushao.mapper.UserInfoMapper;
import com.liushao.service.TransFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2025-01-03
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public boolean isSaveTransFlow(String agentBillNo) {
        QueryWrapper<TransFlow> wrapper = new QueryWrapper<>();
        wrapper.eq("trans_no",agentBillNo);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            return true;
        }

        return false;
    }

    @Override
    public void saveTransFlow(TransFlowBO transFlowBO) {
    /*    TransFlow transFlow = new TransFlow();
        //给属性设置值
        //拷贝
       // BeanUtils.copyProperties(transFlowBO,transFlow);

        String bindCode = transFlowBO.getBindCode();
        //通过bindcode获取用户信息
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("bind_code",bindCode);

        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        transFlow.setUserId(userInfo.getId());
        transFlow.setUserName(userInfo.getName());
        transFlow.setTransType(transFlowBO.getTransTypeEnum().getTransType());
        transFlow.setTransTypeName(transFlowBO.getTransTypeEnum().getTransTypeName());*/

        //获取用户信息
        String bindCode = transFlowBO.getBindCode();
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("bind_code",bindCode);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        //交易流水

        TransFlow tf = new TransFlow();
        tf.setUserId(userInfo.getId());//用户id
        tf.setUserName(userInfo.getName());
        tf.setTransNo(transFlowBO.getAgentBillNo());//交易单号
        tf.setTransType(transFlowBO.getTransTypeEnum().getTransType());
        tf.setTransTypeName(transFlowBO.getTransTypeEnum().getTransTypeName());
        tf.setTransAmount(transFlowBO.getAmount());
        tf.setMemo(transFlowBO.getMemo());

        baseMapper.insert(tf);




    }

    @Override
    public List<TransFlow> selectByUerId(Long userId) {
        QueryWrapper<TransFlow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).orderByDesc("id");
        List<TransFlow> transFlowList = baseMapper.selectList(wrapper);

        return transFlowList;
    }
}
